package com.cal.utms.dao.payment;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.Accounting;
import com.cal.utms.entity.Journalentry;
import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.util.HibernateUtil;

public class JournalEntryDaoImpl implements JournalEntryDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Accounting> getAllAccountings() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		List<Accounting> accountings = null;
		try {
			transaction = session.beginTransaction();
			accountings = session.createQuery("from Accounting A").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return accountings;
	}

	public Object saveJournalEntry(Journalentry journalentry) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Object object = null;
		try {
			transaction = session.beginTransaction();
			object = session.save(journalentry);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return object;
	}

	@SuppressWarnings("unchecked")
	public List<Journalentry> getAllJournals() {
		List<Journalentry> journalentries = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			journalentries = session.createQuery("From Journalentry").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return journalentries;
	}

	public Journalentry getById(int journalId) {
		Journalentry journalentry = null;
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			journalentry = (Journalentry) session.get(Journalentry.class, journalId);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return journalentry;
	}

	public Object updateJournal(Journalentry journalentry, List<MoneyTransaction> moneyTransactions) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Object object = null;
		try {
			transaction = session.beginTransaction();
			object = session.merge(journalentry);
			if (moneyTransactions != null) {
				for (MoneyTransaction moneyTransaction : moneyTransactions) {
					session.save(moneyTransaction);
				}
			}
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
		} finally {
			session.close();
		}
		return object;
	}

}
